####################################################
# TABLES OF REGRESSIONS OF RESCUE MODES BY COUNTRY #
####################################################

# Author: Kasia Nalewajko
# First created: 09 June 2024

rm(list = ls())

# LOAD PACKAGES -----------------------------------------------------------

if (!require("dplyr")) install.packages("dplyr")
if (!require("tidyr")) install.packages("tidyr")
if (!require("stringr")) install.packages("stringr")
if (!require("lmtest")) install.packages("lmtest")
if (!require("sandwich")) install.packages("sandwich")
if (!require("ggplot2")) install.packages("ggplot2")

# LOAD DATA ---------------------------------------------------------------

load("./00 SUBMITTED/00 APSR final/04 replication_files/01 data/RaN.Rda")

# CLEAN DATA ----

ran2 <- ran %>% 
  dplyr::select(file_no, rescuers_no, status, rescuees_no, rescue_country, rescue_loc, rescue_mode) %>% 
  unique()

ran2$hiding <- ifelse(ran2$rescue_mode == "Hiding", 1, 0)
ran2$documents <- ifelse(ran2$rescue_mode == "Providing forged documents", 1, 0)
ran2$goods <- ifelse(ran2$rescue_mode == "Supplying basic goods", 1, 0)
ran2$recruiting <- ifelse(ran2$rescue_mode == "Arranging shelter", 1, 0)
ran2$transfer <- ifelse(ran2$rescue_mode == "Illegal transfer", 1, 0)
ran2$false_evidence <- ifelse(ran2$rescue_mode == "Providing false evidence", 1, 0)
ran2$other <- ifelse(ran2$rescue_mode == "Other", 1, 0)

temp <- ran2 %>% 
  dplyr::group_by(file_no) %>% 
  dplyr::summarise(across(.cols = c(8:14),
                          .fns = ~sum(.)))

temp <- temp %>% 
  mutate(across(.cols = c(2:7),
                .fns = ~ifelse(.>1, 1, .)))

unique_rans <- ran2 %>% 
  dplyr::select(hid, file_no, rescuers_no, status, rescuees_no, rescue_country, rescue_loc) %>% 
  unique()

unique_rans <- left_join(unique_rans, temp, by = "file_no")

only_big <- unique_rans %>% 
  group_by(rescue_country) %>% 
  summarise(rescuers_sum = n()) %>% 
  filter(!is.na(rescue_country) & rescuers_sum > 100) %>% 
  arrange(desc(rescuers_sum))

only_big <- unlist(only_big$rescue_country)

reg <- summary(lm(formula = hiding ~ status + rescue_loc,
                  data = subset(unique_rans, rescue_country == only_big[14])))

# RUN MODELS -----------

# Generate vector of outcomes
outcomes <- c("hiding",
              "recruiting",
              "documents",
              "transfer")

models_list <- vector("list", length(only_big))

# Loop over outcomes
for(i in 1:length(only_big)){
  
  models_list[[i]] <- vector("list", length(outcomes))
  
  for (j in 1:length(outcomes)){
    
    models_list[[i]][[j]] <- vector("list", 2)
    
    # Models with no controls
    models_list[[i]][[j]][[1]] <- lm(as.formula(paste(outcomes[j],"~ status")),
                              data = subset(unique_rans, rescue_country == only_big[i]))

    # Models with locality FE
    models_list[[i]][[j]][[2]] <- lm(as.formula(paste(outcomes[j],"~ status",
                                               "+ as.factor(rescue_loc)")),
                              data = subset(unique_rans, rescue_country == only_big[i]))
  }
  
  print(i)
  
}


# Export Poland ----

modelsummary(list(
  "Hiding" = models_list[[1]][[1]][[1]],
  "Hiding" = models_list[[1]][[1]][[2]], 
  "Network" = models_list[[1]][[2]][[1]],
  "Network" = models_list[[1]][[2]][[2]],
  "Documents" = models_list[[1]][[3]][[1]],
  "Documents" = models_list[[1]][[3]][[2]],
  "Smuggling" = models_list[[1]][[4]][[1]],
  "Smuggling" = models_list[[1]][[4]][[2]]
                  ),
             stars = c('*' = .1, '**' = .05, '***' = .01),
             output = "latex",
             gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
             title = "Generalisability: Linear Probability Models (Poland)",
  coef_omit = "^as\\.factor\\(rescue_loc\\)",
             coef_rename = c("statuscontact" = "Rescuer in contact with insurgents",
                             "statusresister" = "Rescuer-insurgent"
             ))

# Export the Netherlands ----

modelsummary(list(
  "Hiding" = models_list[[2]][[1]][[1]],
  "Hiding" = models_list[[2]][[1]][[2]], 
  "Network" = models_list[[2]][[2]][[1]],
  "Network" = models_list[[2]][[2]][[2]],
  "Documents" = models_list[[2]][[3]][[1]],
  "Documents" = models_list[[2]][[3]][[2]],
  "Smuggling" = models_list[[2]][[4]][[1]],
  "Smuggling" = models_list[[2]][[4]][[2]]
),
stars = c('*' = .1, '**' = .05, '***' = .01),
output = "latex",
gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
title = "Generalisability: Linear Probability Models (the Netherlands)",
coef_omit = "^as\\.factor\\(rescue_loc\\)",
coef_rename = c("statuscontact" = "Rescuer in contact with insurgents",
                "statusresister" = "Rescuer-insurgent"
))

# Export Ukraine ----

modelsummary(list(
  "Hiding" = models_list[[4]][[1]][[1]],
  "Hiding" = models_list[[4]][[1]][[2]], 
  "Network" = models_list[[4]][[2]][[1]],
  "Network" = models_list[[4]][[2]][[2]],
  "Documents" = models_list[[4]][[3]][[1]],
  "Documents" = models_list[[4]][[3]][[2]],
  "Smuggling" = models_list[[4]][[4]][[1]],
  "Smuggling" = models_list[[4]][[4]][[2]]
),
stars = c('*' = .1, '**' = .05, '***' = .01),
output = "latex",
gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
title = "Generalisability: Linear Probability Models (Ukraine)",
coef_omit = "^as\\.factor\\(rescue_loc\\)",
coef_rename = c("statuscontact" = "Rescuer in contact with insurgents",
                "statusresister" = "Rescuer-insurgent"
))

# Export Belgium ----

modelsummary(list(
  "Hiding" = models_list[[5]][[1]][[1]],
  "Hiding" = models_list[[5]][[1]][[2]], 
  "Network" = models_list[[5]][[2]][[1]],
  "Network" = models_list[[5]][[2]][[2]],
  "Documents" = models_list[[5]][[3]][[1]],
  "Documents" = models_list[[5]][[3]][[2]],
  "Smuggling" = models_list[[5]][[4]][[1]],
  "Smuggling" = models_list[[5]][[4]][[2]]
),
stars = c('*' = .1, '**' = .05, '***' = .01),
output = "latex",
gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
title = "Generalisability: Linear Probability Models (Belgium)",
coef_omit = "^as\\.factor\\(rescue_loc\\)",
coef_rename = c("statuscontact" = "Rescuer in contact with insurgents",
                "statusresister" = "Rescuer-insurgent"
))

# Export Hungary ----

modelsummary(list(
  "Hiding" = models_list[[6]][[1]][[1]],
  "Hiding" = models_list[[6]][[1]][[2]], 
  "Network" = models_list[[6]][[2]][[1]],
  "Network" = models_list[[6]][[2]][[2]],
  "Documents" = models_list[[6]][[3]][[1]],
  "Documents" = models_list[[6]][[3]][[2]],
  "Smuggling" = models_list[[6]][[4]][[1]],
  "Smuggling" = models_list[[6]][[4]][[2]]
),
stars = c('*' = .1, '**' = .05, '***' = .01),
output = "latex",
gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
title = "Generalisability: Linear Probability Models (Hungary)",
coef_omit = "^as\\.factor\\(rescue_loc\\)",
coef_rename = c("statuscontact" = "Rescuer in contact with insurgents",
                "statusresister" = "Rescuer-insurgent"
))

# Export Italy ----

modelsummary(list(
  "Hiding" = models_list[[7]][[1]][[1]],
  "Hiding" = models_list[[7]][[1]][[2]], 
  "Network" = models_list[[7]][[2]][[1]],
  "Network" = models_list[[7]][[2]][[2]],
  "Documents" = models_list[[7]][[3]][[1]],
  "Documents" = models_list[[7]][[3]][[2]],
  "Smuggling" = models_list[[7]][[4]][[1]],
  "Smuggling" = models_list[[7]][[4]][[2]]
),
stars = c('*' = .1, '**' = .05, '***' = .01),
output = "latex",
gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
title = "Generalisability: Linear Probability Models (Italy)",
coef_omit = "^as\\.factor\\(rescue_loc\\)",
coef_rename = c("statuscontact" = "Rescuer in contact with insurgents",
                "statusresister" = "Rescuer-insurgent"
))

# Export Czechoslovakia ----

modelsummary(list(
  "Hiding" = models_list[[8]][[1]][[1]],
  "Hiding" = models_list[[8]][[1]][[2]], 
  "Network" = models_list[[8]][[2]][[1]],
  "Network" = models_list[[8]][[2]][[2]],
  "Documents" = models_list[[8]][[3]][[1]],
  "Documents" = models_list[[8]][[3]][[2]],
  "Smuggling" = models_list[[8]][[4]][[1]],
  "Smuggling" = models_list[[8]][[4]][[2]]
),
stars = c('*' = .1, '**' = .05, '***' = .01),
output = "latex",
gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
title = "Generalisability: Linear Probability Models (Czechoslovakia)",
coef_omit = "^as\\.factor\\(rescue_loc\\)",
coef_rename = c("statuscontact" = "Rescuer in contact with insurgents",
                "statusresister" = "Rescuer-insurgent"
))

# Export Lithuania ----

modelsummary(list(
  "Hiding" = models_list[[9]][[1]][[1]],
  "Hiding" = models_list[[9]][[1]][[2]], 
  "Network" = models_list[[9]][[2]][[1]],
  "Network" = models_list[[9]][[2]][[2]],
  "Documents" = models_list[[9]][[3]][[1]],
  "Documents" = models_list[[9]][[3]][[2]],
  "Smuggling" = models_list[[9]][[4]][[1]],
  "Smuggling" = models_list[[9]][[4]][[2]]
),
stars = c('*' = .1, '**' = .05, '***' = .01),
output = "latex",
gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
title = "Generalisability: Linear Probability Models (Lithuania)",
coef_omit = "^as\\.factor\\(rescue_loc\\)",
coef_rename = c("statuscontact" = "Rescuer in contact with insurgents",
                "statusresister" = "Rescuer-insurgent"
))

# Export Germany ----

modelsummary(list(
  "Hiding" = models_list[[10]][[1]][[1]],
  "Hiding" = models_list[[10]][[1]][[2]], 
  "Network" = models_list[[10]][[2]][[1]],
  "Network" = models_list[[10]][[2]][[2]],
  "Documents" = models_list[[10]][[3]][[1]],
  "Documents" = models_list[[10]][[3]][[2]],
  "Smuggling" = models_list[[10]][[4]][[1]],
  "Smuggling" = models_list[[10]][[4]][[2]]
),
stars = c('*' = .1, '**' = .05, '***' = .01),
output = "latex",
gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
title = "Generalisability: Linear Probability Models (Germany)",
coef_omit = "^as\\.factor\\(rescue_loc\\)",
coef_rename = c("statuscontact" = "Rescuer in contact with insurgents",
                "statusresister" = "Rescuer-insurgent"
))

# Export Belarus ----

modelsummary(list(
  "Hiding" = models_list[[11]][[1]][[1]],
  "Hiding" = models_list[[11]][[1]][[2]], 
  "Network" = models_list[[11]][[2]][[1]],
  "Network" = models_list[[11]][[2]][[2]],
  "Documents" = models_list[[11]][[3]][[1]],
  "Documents" = models_list[[11]][[3]][[2]],
  "Smuggling" = models_list[[11]][[4]][[1]],
  "Smuggling" = models_list[[11]][[4]][[2]]
),
stars = c('*' = .1, '**' = .05, '***' = .01),
output = "latex",
gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
title = "Generalisability: Linear Probability Models (Belarus)",
coef_omit = "^as\\.factor\\(rescue_loc\\)",
coef_rename = c("statuscontact" = "Rescuer in contact with insurgents",
                "statusresister" = "Rescuer-insurgent"
))

# Export Yugoslavia ----

modelsummary(list(
  "Hiding" = models_list[[12]][[1]][[1]],
  "Hiding" = models_list[[12]][[1]][[2]], 
  "Network" = models_list[[12]][[2]][[1]],
  "Network" = models_list[[12]][[2]][[2]],
  "Documents" = models_list[[12]][[3]][[1]],
  "Documents" = models_list[[12]][[3]][[2]],
  "Smuggling" = models_list[[12]][[4]][[1]],
  "Smuggling" = models_list[[12]][[4]][[2]]
),
stars = c('*' = .1, '**' = .05, '***' = .01),
output = "latex",
gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
title = "Generalisability: Linear Probability Models (Yugoslavia)",
coef_omit = "^as\\.factor\\(rescue_loc\\)",
coef_rename = c("statuscontact" = "Rescuer in contact with insurgents",
                "statusresister" = "Rescuer-insurgent"
))


# Export Greece ----

modelsummary(list(
  "Hiding" = models_list[[13]][[1]][[1]],
  "Hiding" = models_list[[13]][[1]][[2]], 
  "Network" = models_list[[13]][[2]][[1]],
  "Network" = models_list[[13]][[2]][[2]],
  "Documents" = models_list[[13]][[3]][[1]],
  "Documents" = models_list[[13]][[3]][[2]],
  "Smuggling" = models_list[[13]][[4]][[1]],
  "Smuggling" = models_list[[13]][[4]][[2]]
),
stars = c('*' = .1, '**' = .05, '***' = .01),
output = "latex",
gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
title = "Generalisability: Linear Probability Models (Greece)",
coef_omit = "^as\\.factor\\(rescue_loc\\)",
coef_rename = c("statuscontact" = "Rescuer in contact with insurgents",
                "statusresister" = "Rescuer-insurgent"
))

# Export Russia ----

modelsummary(list(
  "Hiding" = models_list[[14]][[1]][[1]],
  "Hiding" = models_list[[14]][[1]][[2]], 
  "Network" = models_list[[14]][[2]][[1]],
  "Network" = models_list[[14]][[2]][[2]],
  "Documents" = models_list[[14]][[3]][[1]],
  "Documents" = models_list[[14]][[3]][[2]],
  "Smuggling" = models_list[[14]][[4]][[1]],
  "Smuggling" = models_list[[14]][[4]][[2]]
),
stars = c('*' = .1, '**' = .05, '***' = .01),
output = "latex",
gof_omit = "AIC|BIC|RMSE|R2 Within|R2 Within Adj.",
title = "Generalisability: Linear Probability Models (USSR)",
coef_omit = "^as\\.factor\\(rescue_loc\\)",
coef_rename = c("statuscontact" = "Rescuer in contact with insurgents",
                "statusresister" = "Rescuer-insurgent"
))




